package payment.api;

import java.io.IOException;
import java.util.Date;

import com.sun.net.httpserver.HttpExchange;
import com.sun.net.httpserver.HttpHandler;

import payment.Pays;
import payment.Pays.Status;

public class Manage implements HttpHandler {
    /**
     * PUT: token: String, time: int (min) 生成支付订单，成功返回200，失败返回400
     * DELETE: token: String 删除支付订单，成功返回200，失败返回400，无此订单返回404
     * GET: token: String 查询订单状态，成功返回200，未支付返回400，取消返回500，超时返回501，无此订单返回404
     * 其他: 返回404
     */
    @Override
    public void handle(HttpExchange exchange) throws IOException {
        String token = "";
        Status status;
        switch (exchange.getRequestMethod()) {
            case "PUT":
                var args = exchange.getRequestURI().toString().replace("/manage?token=", "").replace("time=", "").split("&");
                Date timeout = new Date(System.currentTimeMillis() + Long.parseLong(args[1]) * 60000l);
                if (Pays.newPay(args[0], timeout)) {
                    exchange.sendResponseHeaders(200, 0);
                    exchange.getResponseBody().write("SUCCESS".getBytes());
                } else {
                    exchange.sendResponseHeaders(400, 0);
                    exchange.getResponseBody().write("FAIL".getBytes());
                }
                break;
            case "DELETE":
                token = exchange.getRequestURI().toString().replace("/manage?token=", "");
                status = Pays.getPayStatus(token);
                if (status == null) {
                    exchange.sendResponseHeaders(404, 0);
                    exchange.getResponseBody().write("NULL".getBytes());
                } else {
                    if (Pays.cancelPay(token)) {
                        exchange.sendResponseHeaders(200, 0);
                        exchange.getResponseBody().write("SUCCESS".getBytes());
                    } else {
                        exchange.sendResponseHeaders(400, 0);
                        exchange.getResponseBody().write("FAIL".getBytes());
                    }
                }
                break;
            case "GET":
                token = exchange.getRequestURI().toString().replace("/manage?token=", "");
                status = Pays.getPayStatus(token);
                switch (status) {
                    case WECHAT:
                    case ALIPAY:
                        exchange.sendResponseHeaders(200, 0);
                        exchange.getResponseBody().write(status.toString().getBytes());
                        break;
                    case NONE:
                        exchange.sendResponseHeaders(400, 0);
                        exchange.getResponseBody().write("NONE".getBytes());
                        break;
                    case CANCEL:
                        exchange.sendResponseHeaders(500, 0);
                        exchange.getResponseBody().write("CANCEL".getBytes());
                        break;
                    case TIMEOUT:
                        exchange.sendResponseHeaders(501, 0);
                        exchange.getResponseBody().write("TIMEOUT".getBytes());
                        break;
                    default:
                        exchange.sendResponseHeaders(404, 0);
                        exchange.getResponseBody().write("NULL".getBytes());
                        break;
                }
                break;
            default:
                exchange.sendResponseHeaders(404, 0);
                break;
        }
        exchange.close();
    }
}
